package customer.design.dispatcher.dispatchermode.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.dom4j.DocumentException;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.Map;

public class XmlTest {
    public static void main(String[] args) {
        String xml = "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"no\"?>\n" +
                "<root>\n" +
                "\t<head>\n" +
                "\t\t<srcCode>123456</srcCode>\n" +
                "\t\t<srcName>jack</srcName>\n" +
                "\t\t<srcAge>15</srcAge>\n" +
                "\t</head>\n" +
                "\t<body>\n" +
                "\t\t<studentName>张三</studentName>\n" +
                "\t\t<studentAge>25</studentAge>\n" +
                "\t\t<classList>\n" +
                "\t\t\t<firstClass>\n" +
                "\t\t\t\t<className>三年级一班</className>\n" +
                "\t\t\t\t<studentCol>50</studentCol>\n" +
                "\t\t\t\t<manNum>26</manNum>\n" +
                "\t\t\t\t<fmailNum>24</fmailNum>\n" +
                "\t\t\t</firstClass>\n" +
                "\t\t\t<firstClass>\n" +
                "\t\t\t\t<className>三年级二班</className>\n" +
                "\t\t\t\t<studentCol>40</studentCol>\n" +
                "\t\t\t\t<manNum>23</manNum>\n" +
                "\t\t\t\t<fmailNum>17</fmailNum>\n" +
                "\t\t\t</firstClass>\n" +
                "\t\t</classList>\n" +
                "\t</body>\n" +
                "</root>";
        String xml2 = "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"no\"?>\n" +
                "<root>\n" +
                "\t<head>\n" +
                "\t\t<srcCode>123456</srcCode>\n" +
                "\t\t<srcName>jack</srcName>\n" +
                "\t\t<srcAge>15</srcAge>\n" +
                "\t</head>\n" +
                "\t<body>\n" +
                "\t\t<studentName>张三</studentName>\n" +
                "\t\t<studentAge>25</studentAge>\n" +
                "\t\t<classList>\n" +
                "\t\t\t<firstClass>\n" +
                "\t\t\t\t<className>三年级一班</className>\n" +
                "\t\t\t\t<studentCol>50</studentCol>\n" +
                "\t\t\t\t<manNum>26</manNum>\n" +
                "\t\t\t\t<fmailNum>24</fmailNum>\n" +
                "\t\t\t</firstClass>\n" +
                "\t\t</classList>\n" +
                "\t\t<classList>\n" +
                "\t\t\t<firstClass>\n" +
                "\t\t\t\t<className>三年级二班</className>\n" +
                "\t\t\t\t<studentCol>40</studentCol>\n" +
                "\t\t\t\t<manNum>23</manNum>\n" +
                "\t\t\t\t<fmailNum>17</fmailNum>\n" +
                "\t\t\t</firstClass>\n" +
                "\t\t</classList>\n" +
                "\t</body>\n" +
                "</root>";
        String json = "{\n" +
                "\t\"head\": {\n" +
                "\t\t\"srcName\": {\n" +
                "\t\t\t\"srcName\": \"jack\"\n" +
                "\t\t},\n" +
                "\t\t\"srcCode\": {\n" +
                "\t\t\t\"srcCode\": \"123456\"\n" +
                "\t\t},\n" +
                "\t\t\"srcAge\": {\n" +
                "\t\t\t\"srcAge\": \"15\"\n" +
                "\t\t}\n" +
                "\t},\n" +
                "\t\"body\": {\n" +
                "\t\t\"studentAge\": {\n" +
                "\t\t\t\"studentAge\": \"25\"\n" +
                "\t\t},\n" +
                "\t\t\"studentName\": {\n" +
                "\t\t\t\"studentName\": \"张三\"\n" +
                "\t\t},\n" +
                "\t\t\"classList\": \n" +
                "\t\t\t [\n" +
                "\t\t\t\t{\n" +
                "\t\t\t\t\t\"studentCol\": {\n" +
                "\t\t\t\t\t\t\"studentCol\": \"50\"\n" +
                "\t\t\t\t\t},\n" +
                "\t\t\t\t\t\"manNum\": {\n" +
                "\t\t\t\t\t\t\"manNum\": \"26\"\n" +
                "\t\t\t\t\t},\n" +
                "\t\t\t\t\t\"fmailNum\": {\n" +
                "\t\t\t\t\t\t\"fmailNum\": \"24\"\n" +
                "\t\t\t\t\t},\n" +
                "\t\t\t\t\t\"className\": {\n" +
                "\t\t\t\t\t\t\"className\": \"三年级一班\"\n" +
                "\t\t\t\t\t}\n" +
                "\t\t\t\t},\n" +
                "\t\t\t\t{\n" +
                "\t\t\t\t\t\"studentCol\": {\n" +
                "\t\t\t\t\t\t\"studentCol\": \"40\"\n" +
                "\t\t\t\t\t},\n" +
                "\t\t\t\t\t\"manNum\": {\n" +
                "\t\t\t\t\t\t\"manNum\": \"23\"\n" +
                "\t\t\t\t\t},\n" +
                "\t\t\t\t\t\"fmailNum\": {\n" +
                "\t\t\t\t\t\t\"fmailNum\": \"17\"\n" +
                "\t\t\t\t\t},\n" +
                "\t\t\t\t\t\"className\": {\n" +
                "\t\t\t\t\t\t\"className\": \"三年级二班\"\n" +
                "\t\t\t\t\t}\n" +
                "\t\t\t\t}\n" +
                "\t\t\t]\n" +
                "\t}\n" +
                "}";
        try {
            Map<String, Object> map = ParseXmlUtil.xml2Map(xml);
            System.out.println(map);
            System.out.println(JSONObject.toJSONString(map));
            XmlInfo xmlInfo = JSONObject.parseObject(json, XmlInfo.class);
//            for (firstClass firstClass : xmlInfo.getBody().getClassList()) {
//
//            }
            System.out.println(xmlInfo);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
class MapUtil{
    public static <T> T map2Bean(final Class<T> clazz, final Map<String, ? extends Object> map) {
        if (map == null) {
            return null;
        }
        T res = null;
        try {
            res = clazz.getDeclaredConstructor().newInstance();
            //获取到所有属性，不包括继承的属性
            Field[] fields = clazz.getDeclaredFields();
            for (Field field : fields) {
                //获取字段的修饰符
                int mod = field.getModifiers();
                if (Modifier.isStatic(mod) || Modifier.isFinal(mod)) {
                    continue;
                }
                //设置对象的访问权限
                field.setAccessible(true);
                //根据属性名称去map获取value
                if(map.containsKey(field.getName())) {
                    //给对象赋值
                    field.set(res, map.get(field.getName()));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return res;
    }
}
